<?php

namespace app\siteadmin\service;

use app\siteadmin\model\User;
use app\Response;

class StoreManageService
{
    /***
     * 快递员列表
    */
    public static function StoreManageListService($a_uid,$page,$num,$enable,$phone,$username,$nickname,$str_time,$end_time,$m_account){
        $adminInfo = User::info(["uid"=>$a_uid]);
        if(!$adminInfo){
            return Response::wrapError(-200,"登录信息异常!");
        }
        if($adminInfo["type"] != 10){
            $paid = $adminInfo["paid"];
            $where[] = ["paid","=",$paid];
        }else{
            $paid = $adminInfo["uid"];
            $where[] = ["paid","=",$paid];
        }
        $where[] = ["type","=",15];
        if(is_numeric($enable)){
            $where[] = ["enable","=",$enable];
        }
        if(!empty($phone)){
            $where[] = ["phone","like","%".$phone."%"];
        }
        if(!empty($username)){
            $where[] = ["username","like","%".$username."%"];
        }
        if(!empty($nickname)){
            $where[] = ["nickname","like","%".$nickname."%"];
        }
        if(!empty($str_time)){
            $where[] = ["addtime",">=",strtotime($str_time)];
        }
        if(!empty($end_time)){
            $endTime = strtotime($end_time)+86399;
            $where[] = ["addtime","<=",$endTime];
        }
        if(!empty($m_account)){
            $m_where[] = ["username|phone","like","%".$m_account."%"];
            $m_account = User::info($m_where);
            if(!empty($m_account)){
                $where[] = ["uid","=",$m_account["paid"]];
            }else{
                $where[] = ["uid","=",0];
            }
        }
        $list = User::datalist($where,$page,$num);
        if(!empty($list)){
            $data = $list->toArray();
            foreach($data as $k => $v){
                $data[$k]["addtime"] = !empty($v["addtime"])?date("Y-m-d H:i:s",$v["addtime"]):"";
                $data[$k]["lasttime"] = !empty($v["lasttime"])?date("Y-m-d H:i:s",$v["lasttime"]):"";
            }
            $count = User::listCount($where);
        }else{
            $data = [];
            $count = 0;
        }
        return Response::wrapData($data,$count);
    }


    /***
     * 快递员修改
    */
    public static function StoreManageEditService($id,$enable,$remarks,$password){
        $userInfo = User::info(["uid"=>$id]);
        if(!$userInfo){
            return Response::wrapError(0,"未检测到该账号");
        }
        if(is_numeric($enable)){
            $data["enable"] = $enable;
            $data["remarks"] = $remarks;
            $res = User::userEdit($id,$data);
        }
        if (!empty($password)){
            $encryption = self::createNonceStr($length = 4);
            $password = self::encPassword($password,$encryption);//密码
            $data["password"] = $password;
            $data["encryption"] = $encryption;
            $res = User::userEdit($id,$data);
        }
        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"修改失败!");
        }
    }
    /***
     * 添加快递员
    */
    public static function StoreManageAddService($a_uid,$data){
        $userInfo = User::info(["phone"=>$data["phone"]]);
        if($userInfo){
            return Response::wrapError(0,"当前手机号已使用");
        }
        $adminInfo = User::info(["uid"=>$a_uid]);
        if(!$adminInfo){
            return Response::wrapError(-200,"登录信息异常!");
        }
        if($adminInfo["type"] != 10){
            $paid = $adminInfo["paid"];
        }else{
            $paid = $adminInfo["uid"];
        }
        $encryption = self::createNonceStr($length = 4);
        $password = self::encPassword($data["password"],$encryption);//密码
        $up_data = [
            "phone" => $data["phone"],
            "password" => $password,
            "encryption" => $encryption,
            "nickname" => $data["nickname"],
            "username" => $data["username"],
            "type" => 15,
            "paid" => $paid,
            "addtime" => time(),
        ];
        $res = User::userAdd($up_data);
        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"添加失败!");
        }
    }

    /**
     * 生成随机字符串
     * @param int $length
     * @return string
     */
    public static function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    //密码加密
    public static function encPassword($password,$encryption){
        $enc = md5(md5($password).$encryption);
        return $enc;
    }
}